import random
import string
import json


def generate_monthly_values(quarter_total, variance=0.1):
    """为季度生成三个月份的值，总和等于季度值"""
    base = quarter_total // 3
    remaining = quarter_total - base * 3

    # 添加一些随机变化
    adjustments = [random.randint(-int(base * variance), int(base * variance)) for _ in range(3)]
    # 确保调整后的总和仍然等于季度值
    adjustments[0] -= sum(adjustments) - remaining

    return [base + adj for adj in adjustments]


def generate_product_data(start_id, count):
    """生成指定范围的产品数据"""
    products = []

    for i in range(start_id, start_id + count):
        # 生成ID，格式为A0001到A5000
        product_id = f"A{i:04d}"

        # 生成年度总销售额 (1000-10000之间)
        total = random.randint(1000, 10000)

        # 分配到四个季度 (q1:20-25%, q2:22-27%, q3:23-28%, q4:25-35%)
        q1_pct = random.uniform(0.20, 0.25)
        q2_pct = random.uniform(0.22, 0.27)
        q3_pct = random.uniform(0.23, 0.28)
        q4_pct = 1.0 - q1_pct - q2_pct - q3_pct  # 确保总和为1

        q1 = round(total * q1_pct)
        q2 = round(total * q2_pct)
        q3 = round(total * q3_pct)
        q4 = total - q1 - q2 - q3  # 确保总和等于年度总销售额

        # 为每个季度生成月度数据
        jan, feb, mar = generate_monthly_values(q1)
        apr, may, jun = generate_monthly_values(q2)
        jul, aug, sep = generate_monthly_values(q3)
        oct, nov, dec = generate_monthly_values(q4)

        # 创建产品对象
        product = {
            id: product_id,
            total: str(total),
            q1: str(q1), "jan": str(jan), "feb": str(feb), "mar": str(mar),
            q2: str(q2), "apr": str(apr), "may": str(may), "jun": str(jun),
            q3: str(q3), "jul": str(jul), "aug": str(aug), "sep": str(sep),
            q4: str(q4), "oct": str(oct), "nov": str(nov), "dec": str(dec)
        }

        products.append(product)

    return products


if __name__ == "__main__":
    # 第一部分：生成2000条数据 (A0001-A2000)
    part1 = generate_product_data(1, 2000)
    print(json.dumps({"products": part1}, indent=2))